(function(factory) {
	if (typeof define === "function" && define.amd) {
		// AMD. Register as an anonymous module.
		define(factory);
	} else {
		// Browser globals
		factory();
	}
}(function() {
	/**
	 * @author 李球 <service@hteos.com>
	 * @class HteOS.Notification 消息通知组件，右上角消息通知
	 * @singleton
	 */
	HteOS.Notification = {

		/**
		 * 初始化消息通知组件
		 */
		init: function() {
			$(document).on("click", ".hte-notification-close", function(event) {
				HteOS.Notification.hide($(this).parent());
				event.stopPropagation();
			});
			this.animate = HteOS.UA.advance;
		},

		/**
		 * 存放所有消息通知
		 *
		 * @property
		 */
		notifications: {},

		length: 0,

		/**
		 * 显示通知，默认的通知栏显示在最下方
		 *
		 * @param {String}
		 *            icon
		 * @param {String}
		 *            title
		 * @param {String}
		 *            message
		 * @param {String}
		 *            hide
		 */
		show: function(icon, title, message, hide) {
			var tpl = "<div class=\"hte-notification hte-notification-animate "+(HteOS.UA.CSS3Transform ? "hte-notification-transform" : "")+"\">" 
						+ "<button type=\"button\" class=\"hte-notification-close close\" >×</button>" 
						+ "<div class=\"hte-notification-icon\">" 
							+ "<img src=\""+HteOS.basePath+"images/logo_64.png\">" 
						+ "</div>" 
						+ "<div class=\"hte-notification-panel\">" 
							+ "<div class=\"hte-notification-title\">{title}</div>" 
							+ "<div class=\"hte-notification-message\">{message}</div>" 
						+ "</div>" 
					+ "</div>";
			tpl = tpl.replace("{title}", title).replace("{message}", message);
			var id = "hte-notification-" + new Date().getTime();
			var notification = $(tpl).attr("id", id);
			this.notifications[id] = notification;
			this.length++;
			
			$(document.body).append(notification);
			/*if (this.animate) {
				notification.fadeIn();
			} else {*/
			if(HteOS.UA.CSS3Transform){
				//notification.show();
				if(!HteOS.UA.Mobile){
					notification.show();
					notification.css("opacity",1);
				}
				window.setTimeout(function() {
					HteOS.Notification.position(notification);
				}, 100);
			}else{
				this.position(notification);
				notification.show();
				notification.css("opacity",1);
			}
			//}
			if (hide) {
				window.setTimeout(function() {
					HteOS.Notification.hide(notification);
				}, 5000);
			}
		},

		/**
		 * 隐藏通知栏，并调整所有通知栏的位置
		 *
		 * @param {Object}
		 *            notification
		 */
		hide: function(notification) {
			delete this.notifications[notification.attr("id")];
			if(HteOS.UA.CSS3Transform){
				this.move(notification,-100);
				window.setTimeout(function(){
					notification.remove();
					collect();
				},200);
			}else{
				if (this.animate) {
					notification.fadeOut(function() {
						$(this).remove();
						collect();
					});
				} else {
					notification.remove();
					collect();
				}
			}
			this.length--;
			if (this.length < 0) {
				this.length = 0;
			}
			
			function collect(){
				var index = 0,notifications = HteOS.Notification.notifications;
				for (var a in notifications) {
					HteOS.Notification.position(notifications[a], index);
					index++;
				}
			}
			
		},
		/**
		 * 调整通知栏的位置
		 *
		 * @param {Object}
		 *            notification
		 * @param {Number}
		 *            index
		 */
		position: function(notification, index) {
			var method = "animate";
			if (index === undefined) {
				method = "css";
				index = this.length - 1;
			}
			if (!this.animate) {
				method = "css";
			}
			//HteOS.UA.Mobile = true;
			if(HteOS.UA.Mobile){
				top = 0;
			}else{
				var top = 30 + index * 100;
				if (index >= 0) {
					top += (index - 1) * 10;
				}
			}
			this.move(notification,top,method);
			if (top + 130 > $(document).height()) {
				var notifications = $(".hte-notification");
				if (notifications.length > 0) {
					HteOS.Notification.hide($(notifications.get(0)));
				}
			}
		},
		
		move : function(notification,top,method){
			if(HteOS.UA.CSS3Transform){
				notification.css("transform","translate3d(0px,"+top+"px,0px)");
			}else{
				notification[method]({
					top: top
				});
			}
		}
	};
}));